<#include "/shared/layout.ftl" />

<@layoutHead title="修改用户权限"></@layoutHead>

<@layoutBody titles=["修改用户权限"]>

<style type="text/css">
    .layui-form-checkbox {
        width: 350px;
    }

    .layui-form-checkbox span {
        width: 93%;
    }
</style>
<fieldset class="layui-elem-field layui-field-title">
    <legend>修改用户权限</legend>
    <input type="hidden" name="userId" value="${user.userId}" />
    <form class="layui-form" action="${baseUrlAdmin}/user/editPermission" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input" value="${user.name}" readonly="readonly" disabled="disabled" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户权限</label>
            <div class="layui-input-block">
                <#list permissions as permission>
                    <input type="checkbox" name="permission[${permission_index}]"
                           title="${permission.remark}-${permission.url}"
                           value="${permission.value}"<#if permission.checked> checked="checked"</#if>
                           lay-filter="itemChoose"/>
                </#list>
                <input type="checkbox" title="全选/全不选" lay-filter="allChoose"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
            </div>
        </div>
    </form>
</fieldset>

<script type="text/javascript">

    layui.use(['form'], function () {
        var form = layui.form, layer = layui.layer;
        form.on('checkbox(allChoose)', function (data) {
            var child = $(data.elem).parent().find('input[type="checkbox"]');
            child.each(function (index, item) {
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
        /**
         form.on('checkbox(itemChoose)', function (data) {
            var sib = $(data.elem).parent().find('input[type="checkbox"]:checked').length;
            var total = $(data.elem).parent().find('input[type="checkbox"]').length;
            if (sib === total) {
                $(data.elem).parent().find('input[type="checkbox"]').prop("checked", true);
                form.render();
            } else {
                $(data.elem).parent().find('input[type="checkbox"]').prop("checked", false);
                form.render();
            }
        });
         **/

        //监听提交
        form.on('submit(formSubmit)', function (e) {
            //JSON.stringify(e.field);
            var valueArray = [];
            var obejct = e.field;
            for (var prop in obejct) {
                valueArray.push(obejct[prop]);
            }
            console.log(valueArray);

            var params = { 'userId': $('input[name="userId"]').val()};
            if (valueArray.length === 0) {
                params['values[]'] = 0;
            } else {
                params['values[]'] = valueArray;
            }
            $.post('${baseUrlAdmin}/user/editPermission', params, function (data) {
                console.log(data);
                layer.alert(data.message);
            });
            return false;
        });
    });
</script>

</@layoutBody>